#include"stack.h"

void  STInit(ST*ps)
{
	assert(ps);
     ps->a = (STDataType*)malloc(N * sizeof(STDataType));
	if (ps->a == NULL)
	{
		perror("malloc fail");
	}
	ps->capacity = N;
	ps->top = -1;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top + 1)
	{
		STDataType* ptr = (STDataType*)malloc((ps->capacity) * 2 * sizeof(STDataType));
		if (ptr == NULL) {
			perror("malloc fail");
			return;
		}
		ps->a = ptr;
		ptr = NULL;
		ps->capacity *= 2;
	}
	ps->a[++ps->top] = x;
}
void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == -1;
}
void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->capacity = 0;
	ps->top = -1;
}
STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top];
}


